package com.krt.webapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.krt.webapp.R;

import cn.bingoogolapple.qrcode.core.QRCodeView;

public class ScanActivity extends AppCompatActivity implements QRCodeView.Delegate{

    private QRCodeView qrCodeView;
    private Button bt1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);

        qrCodeView = (QRCodeView) findViewById(R.id.zxingview);
        qrCodeView.setDelegate(this);

        bt1 = (Button) findViewById(R.id.bt1);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bt1.getText().equals("打开闪光灯")){
                    bt1.setText("关闭闪光灯");
                    qrCodeView.openFlashlight();
                }else {
                    bt1.setText("打开闪光灯");
                    qrCodeView.closeFlashlight();
                }
            }
        });
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Intent intent = new Intent();
        intent.putExtra("scan", result);
        setResult(RESULT_OK,intent);
        finish();
        overridePendingTransition(R.anim.in2, R.anim.out2);
    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }

    @Override
    protected void onStop() {
        bt1.setText("打开闪光灯");
        qrCodeView.closeFlashlight();
        qrCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onStart() {
        super.onStart();
        qrCodeView.startCamera();
    }

    @Override
    protected void onResume() {
        super.onResume();
        qrCodeView.startSpot();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        qrCodeView.onDestroy();
    }
}
